﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;

namespace KaiQuan.Web
{
    public partial class resources : System.Web.UI.Page
    {
        #region "字段"

        /// <summary>
        /// 缓存所有的资源文件
        /// </summary>
        private static Dictionary<Int32, String> allFileData = null;

        private static Object locker = new object();

        private static String resourceFilePath = "~/productResource";

        private static String fileHtmlTemplate = "<li><a href='resources.aspx?fileid={0}'>{1}</a></li><br>";

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                String fileid = Request.QueryString["fileid"];
                if (String.IsNullOrEmpty(fileid))
                {
                    InitialFileData();
                    MakeResourceFileHtml();
                }
                else
                {
                    WriteFile(fileid);
                }
            }
        }

        /// <summary>
        /// 向客户端输出文件
        /// </summary>
        /// <param name="fileid"></param>
        protected void WriteFile(String fileid)
        {
            try
            {
                Int32 id = -1;
                if (Int32.TryParse(fileid, out  id) && allFileData.ContainsKey(id))
                {
                    String physicalPath = Server.MapPath(resourceFilePath);
                    String filePath = Path.Combine(physicalPath, allFileData[id]);
                    Byte[] buffer = new Byte[1024 * 100];
                    using (FileStream fs = new FileStream(filePath,
                                            FileMode.Open,
                                            FileAccess.Read,
                                            FileShare.Read))
                    {
                        Int32 readLegth = 0;
                        do
                        {
                            readLegth = fs.Read(buffer, 0, buffer.Length);
                            if (readLegth > 0)
                                Response.OutputStream.Write(buffer, 0, readLegth);
                        } while (readLegth > 0);
                        Response.Flush();
                    }
                }
            }
            catch (Exception)
            { }
        }

        /// <summary>
        /// 创建资源文件的html
        /// </summary>
        protected void MakeResourceFileHtml()
        {
            if (allFileData == null) return;
            StringBuilder builder = new StringBuilder();
            foreach (KeyValuePair<Int32, String> item in allFileData)
                builder.AppendFormat(fileHtmlTemplate, item.Key, item.Value);

            lt_resourceFile.Text = builder.ToString();
        }

        /// <summary>
        /// 初始化所有的资源文件
        /// </summary>
        private void InitialFileData()
        {
            try
            {
                if (allFileData == null)
                {
                    lock (locker)
                    {
                        if (allFileData != null) return;
                        allFileData = new Dictionary<int, string>();
                        String physicalPath = Server.MapPath(resourceFilePath);
                        String[] files = null;
                        MyIO.TryGetFiles(physicalPath,out files);
                        if (files != null)
                        {
                            for (Int32 i = 0; i < files.Length; i++)
                                allFileData.Add(i, MyString.RightOfLast(files[i], "\\"));
                        }
                    }
                }
            }
            catch (Exception)
            {  }
        }
    }
}